package irie.search;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.ansj.splitWord.analysis.ToAnalysis;
import org.apache.log4j.BasicConfigurator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class Query {
	static String reg = "[^\u4e00-\u9fa5]";
	static int rsnum;
	static int vectorcount;
	static List<HTMLentity> list;
	double[][] W;
	Map<String, Integer> map;

	public void initialize() {
		HTMLAnalyser ha = new HTMLAnalyser();
		W = ha.analyse();
		rsnum = ha.htmlnum;
		list = ha.list;
		map = ha.map;
		vectorcount = ha.vectorcount;
	}

	public double[] search(String str) {
		String s = ToAnalysis.parse(str).toString();
		String s1[] = s.split(",");
		// HTMLAnalyser ha = new HTMLAnalyser();
		// double[][] W = ha.analyse();
		// rsnum = ha.htmlnum;
		double[] Q = new double[vectorcount];
		for (int i = 0; i < Q.length; i++)
			Q[i] = 0;
		// list = ha.list;
		// Map<String, Integer> map = ha.map;
		for (int i = 0; i < s1.length; i++) {
			if (s1[i].contains("/n")) {
				s1[i] = s1[i].replaceAll(reg, "");
				if (map.containsKey(s1[i])) {
					int qloc = map.get(s1[i]) - 1;
					Q[qloc]++;
				}
			}
		}
		// 计算相似度
		double[] up = new double[rsnum];
		double[] downlf = new double[rsnum];
		double downrg = 0;
		for (int dc = 0; dc < rsnum; dc++)
			for (int i = 0; i < vectorcount; i++) {
				up[dc] += W[dc][i] * Q[i];
				downlf[dc] += Math.pow(W[dc][i], 2);
			}
		for (int i = 0; i < vectorcount; i++) {
			downrg += Math.pow(Q[i], 2);
		}
		double[] rs = new double[rsnum];
		for (int i = 0; i < rsnum; i++) {
			if (up[i] == 0)
				rs[i] = 0;
			else
				rs[i] = up[i] / Math.sqrt(downlf[i] * downrg);
		}

		// TEST
		// for (int i = 0; i < rsnum; i++) {
		// System.out.println(rs[i]);
		// }
		return rs;
	}

	public double[][] rshandler(double[] rs) {
		// 构造排序后的二维数组
		double[][] p = new double[rsnum][2];
		double[][] sim = new double[rsnum][2];
		int[] flag = new int[rsnum];
		for (int i = 0; i < rs.length; i++) {
			p[i][0] = rs[i];
			p[i][1] = i + 1;
			flag[i] = 0;
		}
		Arrays.sort(rs);
		for (int i = 0; i < rsnum; i++) {
			double token = rs[i];
			for (int j = 0; j < rsnum; j++) {
				if (p[j][0] == token && flag[j] == 0) {
					sim[i][0] = j + 1;
					sim[i][1] = token;
					flag[j] = 1;
					break;
				}
			}
		}
		return sim;
	}
	
	private static String readString(int n) {
		StringBuffer str = new StringBuffer("");
		File file = new File("D:\\webmagic\\mm.10086.cn", n + ".txt");
		try {
			FileReader fr = new FileReader(file);
			int ch = 0;
			while ((ch = fr.read()) != -1) {
				str.append((char) ch);
			}
			fr.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("File reader出错");
		}
		return str.toString();
	}

	public String ex(int n) throws Exception{
		StringBuffer result = new StringBuffer("");
		StringBuffer s = new StringBuffer("");
		String str = this.readString(n);
		String str1 = str;
		//int loc=str.indexOf("div");
		int loc1=str.indexOf("<div class=\"mj_info font-f-yh\">");
		int loc2=str.indexOf("<!--含可选收费项目begin-->");
		//System.out.println(loc1+","+loc2);
		str=str.substring(loc1, loc2);
		loc1=str.indexOf("</span></li>");
		loc2=str.lastIndexOf("</li>");
		str=str.substring(loc1+13,loc2);
		//System.out.println(str);
		loc1=str.indexOf("<a");
		loc2=str.lastIndexOf("\">");
		String a=str.substring(1,loc1);
		String b=str.substring(loc2+2,str.length());
		s.append(a);s.append(b);
		str=s.toString().replaceAll(" ","");
		str=str.toString().replaceAll("<li>","");
		str=str.toString().replaceAll("</li>","");
		str=str.toString().replaceAll("</a>","");
		loc1=str1.indexOf("<title>");
		loc2=str1.indexOf("-Mobile Market应用商场");
		str1=str1.substring(loc1+7,loc2);
		//System.out.println(loc1);
	    result.append("应用名称："+str1+'\n');
		result.append(str);
	    //System.out.println(str);
		return result.toString();
	}

	public static void main(String[] args) {
		BasicConfigurator.configure();
		// String s = "农业";
		// String s = new Scanner(System.in).nextLine();
		long ts = System.currentTimeMillis();
		Query q = new Query();
		q.initialize();
		long te = System.currentTimeMillis();
		System.out.println();
		System.out.print("初始化完成！文档总数：" + rsnum + ",向量维度：" + vectorcount);
		System.out.println(",初始化耗时："+(te-ts)+" ms");
		for (;;) {
			int ci=0;
			System.out.println();
			System.out.println("请输入检索内容(输入exit结束)：");
			String s = new Scanner(System.in).nextLine();
			if (s.equals("exit"))
				break;
			long ts1 = System.currentTimeMillis();
			double[][] sim = q.rshandler(q.search(s));
			int nilflag=0;
			for(int i=0;i<rsnum;i++){
				if(sim[i][1]!=0) nilflag=1;
			}
			if(nilflag==0) {
				System.out.println("没有找到结果！");
				continue;
			}
			System.out.println("HTML标号----相似度(从小到大)----URL地址");
			for (int i = 0; i < rsnum; i++) {
				if (sim[i][1] != 0&&ci!=10) {
					System.out.print((int) sim[i][0]);
					System.out.print("----");
					System.out.print(sim[i][1]);
					System.out.print("----");
					System.out.println("URL:" + list.get((int) sim[i][0] - 1).getURL());
					try {
						System.out.println("信息抽取结果：");
						System.out.println(q.ex((int) sim[i][0]));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						//e.printStackTrace();
					}
					ci++;
				}
			}
			long te1 = System.currentTimeMillis();
		    //System.out.println(q.ex((int)sim[rsnum-1][0]));
			System.out.println();
			System.out.println("耗时："+(te1-ts1)+" ms");
		}
		System.out.println("系统已关闭！");
	}

}
